package agents.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import ontology.concepts.vo.Antecedent;
import ontology.concepts.vo.VONorm;
import ontology.concepts.vo.VOObligation;

public class VOCaseSeed {
	private List<VONorm> norms; //list with norms with possible violations
	private List<String> parties;
	private Set<PossibleObligation> possibleObligations;
	private static Random rand = new Random(System.currentTimeMillis());
	
	public VOCaseSeed(List<VONorm> norms, List<String> parties, Set<PossibleObligation> possibleObligations){
		this.norms = norms;
		this.parties = parties;
		this.possibleObligations = possibleObligations;
	}
	
	public List<VONorm> getNorms() {
		return norms;
	}

	public void setNorms(List<VONorm> norms) {
		this.norms = norms;
	}

	public Set<PossibleObligation> getPossibleObligations() {
		return possibleObligations;
	}

	public void setPossibleObligations(Set<PossibleObligation> possibleObligations) {
		this.possibleObligations = possibleObligations;
	}

	
	public VONorm getRandomVONorm() {
		if(norms.size()>0){
			int index = rand.nextInt(norms.size());
			return norms.get(index);
		}
		else 
			return null;
	}

	public static VOObligation getRandomViolation(VONorm norm) {
		Set<VOObligation> obligations = norm.getConsequent().getPrescription();
		if(obligations.size() > 0 ){
			int index = rand.nextInt(obligations.size());
			VOObligation obligation = null;
			Iterator<VOObligation> iterator = obligations.iterator();
			for(int i = 0; i <= index && iterator.hasNext(); i++){
				obligation = iterator.next();
			}
			return obligation;
		}
		return null;
	}
	
	public static HashSet<VOObligation> getRandomSolution(VOCaseSeed seed, int maxObligations){
		HashSet<VOObligation> solution = new HashSet<VOObligation>();
		int n = (int) rand.nextInt(maxObligations);
		if(n == 0)
			n = 1;
		generateSlices(seed.getPossibleObligations());
		for(int i = 0; i < n; i++){
			VOObligation  pres = spinRoullete(seed.getPossibleObligations());
			solution.add(pres);
		}
		return solution;
	}
	
	public static VOObligation spinRoullete(Set<PossibleObligation> set){
		VOObligation pres = null;
		double prob = rand.nextDouble()*100;
		for (PossibleObligation possibleObligation : set) {
			if( prob >= possibleObligation.getSliceB() && prob <= possibleObligation.getSliceE()){
				pres = possibleObligation.getObligation().clone();
				break;
			}
		}
		return pres; 
	}
	
	public static boolean  generateSlices(Set<PossibleObligation> set){
		float totalProb = 0;
		for (PossibleObligation possibleObligation : set) {
			totalProb += possibleObligation.getProb();
		}
		if(totalProb == 0)
			return false;
		else{
			float lastProb = 0;
			for(PossibleObligation possibleObligation : set){
				possibleObligation.setSliceB(lastProb);
				lastProb += (possibleObligation.getProb()/totalProb)*100;
				possibleObligation.setSliceE(lastProb);
			}	
		return true;
		}
	}

	public List<String> getRandomParties() {
		ArrayList<String> part = new ArrayList<String>();
		int n = rand.nextInt(parties.size());
		for(int i = 0; i < n; i++){
			int index = rand.nextInt(parties.size());
			part.add(new String(parties.get(index)));
		}
		return part;
	}
}
